﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * 
 * */

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using QWEntity.Local;
using QWModel.Properties;
using QWRepository.Local;
using Timer = System.Threading.Timer;

namespace QWModel
{
    /// <summary>
    /// WClock : UserControl
    /// </summary>
    public partial class QWClock : UserControl
    {
        private Timer _timer;
        private Timer _tmrCompany;

        /// <summary>
        /// Initializes a new instance of the <see cref="QWClock"/> class.
        /// </summary>
        public QWClock()
        {
            InitializeComponent();

            // set Clock Timer
            _timer = new Timer(TimeWorker, null, 1000, 1000);
            _tmrCompany = new Timer(SetCompany, null, 1000, 300000);

            Parallel.Invoke(SetBackgroundImage);

            // set MRG
            mrg.InnerCircleRadius = 16;
            mrg.OuterCircleRadius = 20;
            mrg.SpokeThickness = 5;
            mrg.NumberSpoke = 15;
            mrg.RotationSpeed = 1000;
            mrg.Active = true;
        }

        /// <summary>
        /// Sets the company.
        /// </summary>
        /// <param name="stateInfo">The state info.</param>
        private void SetCompany(object stateInfo)
        {
            try
            {
                if (lblCompanyName.InvokeRequired)
                {
                    var o = new DelegateTimer(SetCompany);
                    lblCompanyName.Invoke(o, stateInfo);
                }
                else
                {
                    List<Company> cmp = new CompanyBase().GetAll();
                    if (cmp != null)
                    {
                        lblCompanyName.Text = cmp[0].CompanyName;
                        lblCompanyDescription.Text =
                            string.Format("{0} {1}\r\nPhone: {2} Fax: {3}\r\nEmail: {4} ",
                                          cmp[0].Address, cmp[0].City, cmp[0].Phone,
                                          cmp[0].Fax, cmp[0].Email);
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// Sets the background image.
        /// </summary>
        private void SetBackgroundImage()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(SetBackgroundImage);
                Invoke(o);
            }
            else
            {
                BackgroundImage = Resources.widget_black320;
            }
        }

        /// <summary>
        /// Times the worker.
        /// </summary>
        /// <param name="stateInfo">The state info.</param>
        private void TimeWorker(object stateInfo)
        {
            try
            {
                if (InvokeRequired)
                {
                    var o = new DelegateTimer(TimeWorker);
                    Invoke(o, stateInfo);
                }
                else
                {
                    lblDate.Text = DateTime.Now.ToString("dddd, dd MMMM yyyy");
                    lblTime.Text = DateTime.Now.ToString("HH:mm:ss");
                }
            }
            catch (Exception)
            {
            }
        }

        #region Nested type: DelegateThis

        /// <summary>
        /// DelegateThis
        /// </summary>
        private delegate void DelegateThis();

        #endregion

        #region Nested type: DelegateTimer

        /// <summary>
        ///  DelegateTimer
        /// </summary>
        private delegate void DelegateTimer(object stateInfo);

        #endregion
    }
}